﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class OrderApiController : ApiController
    {
        public OrderRepository Get(int? pageNumber, string orderBy, string orderDirection, string orderCode, string partnerID, string partnerName, string status, string datefrom, string dateto)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(orderCode))
                    whereClause = String.Format(whereClause + " AND orderCode.Contains(\"{0}\")", orderCode);
                if (!String.IsNullOrWhiteSpace(partnerName))
                    whereClause = String.Format(whereClause + " AND partnerName.Contains(\"{0}\")", partnerName);
                if (!String.IsNullOrWhiteSpace(status))
                    whereClause = String.Format(whereClause + " AND status.Contains(\"{0}\")", status);
                if (!String.IsNullOrWhiteSpace(partnerID))
                    whereClause = String.Format(whereClause + " AND partnerID = {0}", partnerID);

                if (datefrom.Contains("NaN") || dateto.Contains("NaN")) return new OrderRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);

                return new OrderRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1, ConvertUtility.DateTimeParse(datefrom), ConvertUtility.DateTimeParse(dateto));
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        //public OrderRepository Get(int? pageNumber, string orderBy, string orderDirection, string orderCode, string partnerName, string status)
        //{
        //    try
        //    {
        //        string whereClause = "IsDeleted != true";
        //        if (!String.IsNullOrWhiteSpace(orderCode))
        //            whereClause = String.Format(whereClause + " AND orderCode.Contains(\"{0}\")", orderCode);
        //        if (!String.IsNullOrWhiteSpace(partnerName))
        //            whereClause = String.Format(whereClause + " AND partnerName.Contains(\"{0}\")", partnerName);
        //        if (!String.IsNullOrWhiteSpace(status))
        //            whereClause = String.Format(whereClause + " AND status.Contains(\"{0}\")", status);

        //        return new OrderRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
        //    }
        //    catch (Exception e)
        //    {
        //        throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
        //    }
        //}

        public List<Order> Get()
        {
            try
            {
                return new OrderRepository().GetListByStatus("N");
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public Order Get(int? id)
        {
            try
            {
                var order = new Order();
                var orderRepository = new OrderRepository();
                order.OrderCode = GenerateCode.GetCode(orderRepository.GetNextIndex(), CodeType.Order);
                if (!(!id.HasValue || id == 0))
                {
                    order = orderRepository.GetByID(id.Value);
                }
                var partnerRepository = new PartnerRepository();
                order.Partners = partnerRepository.GetListPartner();

                var orderlineRepository = new OrderLineRepository();
                order.OrderLines = orderlineRepository.GetList(id);
                return order;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var orderRepository = new OrderRepository();
                    orderRepository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public OrderRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string orderCode, string partnerName, string status,string datefrom,string dateto)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');

                var orderRepository = new OrderRepository();
                orderRepository.Delete(idArr);

                return Get(pageNumber, orderBy, orderDirection,orderCode,string.Empty,partnerName,status, datefrom, dateto);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public Order Put(Order order)
        {
            try
            {
                var orderRepository = new OrderRepository();
                int orderID = orderRepository.Update(order);

                return Get(orderID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }
    }
}
